package com.usefullc.user.service.component;

import com.usefullc.common.constants.SubPubConstants;
import com.usefullc.common.util.JsonUtil;
import com.usefullc.system.service.JobTemplateService;
import com.usefullc.system.service.component.SystemConfigComponent;
import com.usefullc.system.service.component.redis.SubPubMessage;
import com.usefullc.system.service.component.redis.SubPubMessageConsume;
import com.usefullc.system.service.component.scheduler.IQuartzHelper;
import com.usefullc.user.domain.User;
import com.usefullc.user.enums.UserLevelEnum;
import com.usefullc.user.query.UserQuery;
import com.usefullc.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户过期检测处理
 * @author: Sampson
 * @date: 2022-02-01 20:05
 */
@Slf4j
@Service
public class UserExpireConsume implements SubPubMessageConsume {

    @Autowired
    private SystemConfigComponent systemConfigComponent;

    @Autowired
    private JobTemplateService jobTemplateService;

    @Autowired
    private UserService userService;


    @Autowired
    private IQuartzHelper quartzHelper;


    @Override
    public void consume(SubPubMessage subPubMessage) {
        if(!systemConfigComponent.getAppName().equals(subPubMessage.getAppName())){
            return;
        }

        String messageKey = SubPubConstants.JOB+"_"+ 1 +"_";
        if(StringUtils.startsWith(subPubMessage.getKey(),messageKey)){
            Object data = subPubMessage.getData();
            UserQuery userQuery = JsonUtil.json2Bean(data.toString(), UserQuery.class);
//            HashMap<String, Object> dataMap = (HashMap<String, Object>) fireDeviceQuery1

            Long jobTemplateId = Long.valueOf(StringUtils.removeStart(subPubMessage.getKey(),messageKey));
            try{
                log.info("user expire consume start,jobTemplateId={},参数={}",jobTemplateId,data);

                List<User> userList = userService.findAll(userQuery);
                if (CollectionUtils.isEmpty(userList)) {
                    return;
                }
                long nowTime = System.currentTimeMillis();
                for (User user : userList) {
                    if(user.getLevel() == UserLevelEnum.UNLIMITED.getCode()){
                        continue;
                    }
                    Integer expireDay = Integer.valueOf(DateFormatUtils.format(user.getGmtExpire().getTime(),"yyyyMMdd"));
                    Integer nowDay = Integer.valueOf(DateFormatUtils.format(nowTime,"yyyyMMdd"));
                    if(expireDay < nowDay){
                        user.setLevel(UserLevelEnum.NORMAL.getCode()); //变回普通用户
                        userService.save(user);  // todo 所有一起修改掉，暂不考虑并发覆盖情况
                    }
                }
            }catch (Exception e){
                log.error("consume job category failed",e);
            }finally {
                quartzHelper.complete(jobTemplateId.toString());
            }


        }

    }
}
